<?php

use yii\db\Migration;

/**
 * Class m171127_030722_createTableFile
 */
class m171127_030722_createTableFile extends Migration
{
    /**
     * @inheritdoc
     */
    public function safeUp()
    {
        $tableOptions = null;
        if ($this->db->driverName === 'mysql') {
            // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
            $tableOptions = 'CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE=InnoDB';
        }

        $this->createTable('{{%file}}', [
            'id' => $this->string(50)->notNull()->defaultValue('')->comment('课程ID'),
            'user_id' => $this->integer()->notNull()->defaultValue(0)->comment('用户ID'),

            'filename' => $this->string(50)->notNull()->defaultValue('')->comment('附件名称'),
            'size' => $this->string(20)->notNull()->defaultValue('')->comment('附件大小'),
            'type' => $this->string(10)->notNull()->defaultValue('')->comment('附件类型'),
            'hash' => $this->string(100)->notNull()->defaultValue('')->comment('检验码'),
            'server' => $this->string(100)->notNull()->defaultValue('')->comment('服务器'),
            'key' => $this->string(100)->notNull()->defaultValue('')->comment('服务端名称'),
            'sourcekey' => $this->string(100)->notNull()->defaultValue('')->comment('源文件名称'),
            'num' => $this->smallInteger()->notNull()->defaultValue(1)->comment('文件数量'),
            'persistentid' => $this->string(100)->notNull()->defaultValue('')->comment('视频转换码'),

            'status' => $this->smallInteger()->notNull()->defaultValue(10)->comment('状态'),
            'created_at' => $this->integer()->notNull()->defaultValue(0)->comment('创建时间'),
            'updated_at' => $this->integer()->notNull()->defaultValue(0)->comment('更新时间'),

        ], $tableOptions);

        $this->addCommentOnTable('{{%file}}','文件附件表');

        $this->addPrimaryKey('file_id','{{%file}}','id');
    }

    /**
     * @inheritdoc
     */
    public function safeDown()
    {
        $this->dropTable('{{%file}}');
    }

    /*
    // Use up()/down() to run migration code without a transaction.
    public function up()
    {

    }

    public function down()
    {
        echo "m171127_030722_createTableFile cannot be reverted.\n";

        return false;
    }
    */
}
